package tw.com.book;

import com.parse.ParseUser;

import tw.com.book.api.task01.MainTaskService;
import tw.com.book.componet.DBContract;
import tw.com.book.core.task01.MainTaskServiceImpl;
import tw.com.book.domain.V001Type;
import tw.com.book.dto.MainDTO;
import tw.com.book.task01.AddIssueActivity;
import tw.com.book.task01.EditIssueActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private final static int ADD_CODE = 0001;
	private final static int EDIT_CODE = 002;
	private final static String TAG = MainActivity.class.getName();

	private MainTaskService service = new MainTaskServiceImpl();

	private MainDTO dto = new MainDTO();

	private ListView issuesListView;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub

		if (requestCode == ADD_CODE) {
			service.doQuery(dto, this);

			this.update();
		} else {

		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	private void update() {
		IssueAdapter addapter = (IssueAdapter) issuesListView.getAdapter();
		addapter.notifyDataSetChanged();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// this.issueView=new IssueView(this);
		// this.setContentView(issueView);
		setContentView(R.layout.activity_main);

		this.issuesListView = (ListView) this.findViewById(R.id.issuesList);
		this.service.doQuery(dto, this);
		this.issuesListView.setAdapter(new IssueAdapter());
		this.issuesListView.setOnItemClickListener(new EditClick());

	}

	/**
	 * 索引顯示
	 * 
	 * @author Andy
	 * 
	 */
	private class EditClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			dto.setSelectV001(dto.getV001s().get(arg2));
			Intent it = new Intent(MainActivity.this, EditIssueActivity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable(DBContract.CREATE_VOO1, dto.getSelectV001());
			it.putExtras(bundle);
			startActivityForResult(it, EDIT_CODE);

		}

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.addItem:
			Intent it = new Intent(MainActivity.this, AddIssueActivity.class);

			startActivityForResult(it, ADD_CODE);
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);

		return true;
	}

	private class IssueAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return dto.getV001s().size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return dto.getV001s().get(arg0);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView textView = new TextView(MainActivity.this);
			textView.setText(dto.getV001s().get(position).getTitle().toString());

			LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View layout = mInflater.inflate(R.layout.issue_index_view, null);

			final TextView title = (TextView) layout.findViewById(R.id.title);
//			final TextView yyyymmdd = (TextView) layout
//					.findViewById(R.id.createDate);
			// final TextView hhmmss = (TextView) layout
			// .findViewById(R.id.createTime);

			final V001Type v001 = dto.getV001s().get(position);

			title.setText(v001.getTitle());
//			yyyymmdd.setText(v001.getCreateDate());
			// hhmmss.setText(v001.getCreateTime());

			// TODO Auto-generated method stub
			return layout;
		}

		@Override
		public void notifyDataSetChanged() {
			// TODO Auto-generated method stub
			super.notifyDataSetChanged();
		}
	}

}
